/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.engine.core.action.modify;

import com.inspur.edp.bff.api.manager.context.ModifyContext;
import com.inspur.edp.bff.api.manager.context.RetrieveContext;
import com.inspur.edp.bff.spi.action.modify.AbstractModifyAction;
import com.inspur.edp.bff.spi.action.modify.AfterModifyAction;
import com.inspur.edp.bff.spi.action.modify.BeforeModifyAction;
import com.inspur.edp.bff.spi.action.modify.DefaultModifyAction;
import com.inspur.edp.bff.spi.action.retrieve.AbstractRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.AfterRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.DefaultRetrieveAction;
import com.inspur.edp.bff.spi.assembler.AbstractExtendModifyAssembler;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.collection.VMActionCollection;
import com.inspur.edp.formserver.viewmodel.common.InternalExtendActionUtil;

import java.util.ArrayList;

import static com.inspur.edp.bff.engine.core.action.BffEngineAssemblerBase.getActions;

public class BffEngineModifyAssembler extends AbstractExtendModifyAssembler {

    private GspViewModel vo;

    public BffEngineModifyAssembler(GspViewModel vo) {
        this.vo = vo;
    }

    @Override
    protected ArrayList<BeforeModifyAction> getBeforeModifyActions(ModifyContext modifyContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getBeforeModifyActions();
        return getActions(BeforeModifyAction.class, actions, getParamTypes(), getParamValues(modifyContext), null, null);
    }

    @Override
    protected ArrayList<AbstractModifyAction> getModifyActions(ModifyContext modifyContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getModifyActions();
        return getActions(
                AbstractModifyAction.class,
                actions,
                getParamTypes(),
                getParamValues(modifyContext),
                new DefaultModifyAction(modifyContext),
                InternalExtendActionUtil.ModifyActionId);
    }

    @Override
    protected ArrayList<AfterModifyAction> getAfterModifyActions(ModifyContext modifyContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getAfterModifyActions();
        return getActions(AfterModifyAction.class, actions, getParamTypes(), getParamValues(modifyContext), null, null);
    }

    private ArrayList<Class> getParamTypes() {
        ArrayList<Class> paramTypes = new ArrayList<>();
        paramTypes.add(ModifyContext.class);
        return paramTypes;
    }

    private ArrayList<Object> getParamValues(ModifyContext modifyContext) {
        ArrayList<Object> values = new ArrayList<>();
        values.add(modifyContext);
        return values;
    }
}
